package com.taomu.app.zongchouhuli.utils.adpter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.utils.entity.OrderExplainEntity;
import com.taomu.app.zongchouhuli.utils.entity.SelectBalanceEntity;

import java.util.ArrayList;
import java.util.List;

public class OrderDialogBalanceExplainAdp extends RecyclerView.Adapter<OrderDialogBalanceExplainAdp.OrderDialogBalanceExplainHolder>{

    private List<OrderExplainEntity> mData;

    private List<OrderExplainEntity> mSelectData;

    private Context mContext;

    private LayoutInflater inflater;

    public OrderDialogBalanceExplainAdp(Context mContext){
        this.mContext = mContext;
        this.mData = new ArrayList<>();
        this.mSelectData = new ArrayList<>();
        this.inflater = LayoutInflater.from(mContext);
    }

    public void setData(List<OrderExplainEntity> mData){
        this.mData.clear();
        this.mData.addAll(mData);
        notifyDataSetChanged();
    }

    public void setData(List<OrderExplainEntity> mData,OrderExplainEntity data){
        this.mData.clear();
        this.mData.addAll(mData);
        if(data != null){
            mSelectData.clear();
            mSelectData.add(data);
        }
        notifyDataSetChanged();
    }

    public void addData(List<OrderExplainEntity> mData){
        this.mData.addAll(mData);
        notifyDataSetChanged();
    }

    @Override
    public OrderDialogBalanceExplainHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_dialog_order_balance_explain,parent,false);
        return new OrderDialogBalanceExplainHolder(view);
    }

    @Override
    public void onBindViewHolder(OrderDialogBalanceExplainHolder holder, int position) {
        final OrderExplainEntity entity = mData.get(position);
        holder.update(entity);
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class OrderDialogBalanceExplainHolder extends RecyclerView.ViewHolder{

        private TextView mBalanceTypeTv;

        private TextView mBalanceRadioTv;

        private TextView mContentTv;

        public OrderDialogBalanceExplainHolder(View itemView) {
            super(itemView);
            mBalanceTypeTv = itemView.findViewById(R.id.item_dialog_order_balance_explain_balance_type_tv);
            mBalanceRadioTv = itemView.findViewById(R.id.item_dialog_order_balance_explain_radio_tv);
            mContentTv = itemView.findViewById(R.id.item_dialog_order_balance_explain_content_tv);
        }

        public void update(OrderExplainEntity entity){
            mBalanceRadioTv.setText(entity.getRadioName());
            mBalanceTypeTv.setText(entity.getTypeName());
            if(entity.getTypeName().contains("日结")){
                mContentTv.setText("每天结算一次,日收益率");
            }else if(entity.getTypeName().contains("周结")){
                mContentTv.setText("每周结算一次,日收益率");
            }else if(entity.getTypeName().contains("月结")){
                mContentTv.setText("每月结算一次,日收益率");
            }
            if(isSelect(entity)){
                mBalanceTypeTv.setTextColor(mContext.getResources().getColor(R.color.base_blue));
                mContentTv.setTextColor(mContext.getResources().getColor(R.color.text_normal_black));
                mBalanceRadioTv.setTextColor(mContext.getResources().getColor(R.color.base_red));
            }else{
                mBalanceTypeTv.setTextColor(mContext.getResources().getColor(R.color.text_light_gray));
                mContentTv.setTextColor(mContext.getResources().getColor(R.color.text_light_gray));
                mBalanceRadioTv.setTextColor(mContext.getResources().getColor(R.color.text_light_gray));
            }
        }
    }

    public void checkSelect(OrderExplainEntity entity){
        mSelectData.clear();
        mSelectData.add(entity);
        notifyDataSetChanged();
    }

    public boolean isSelect(OrderExplainEntity entity){
        return mSelectData.contains(entity);
    }

    public List<OrderExplainEntity> getSelectData(){
        return mSelectData;
    }

    public void selectCheck(OrderExplainEntity entity){
        if(mData != null){
            for(int i=0;i<mData.size();i++){
                if(mData.get(i).getTypeid().equals(entity.getTypeid())){
                    mSelectData.clear();
                    mSelectData.add(mData.get(i));
                }
            }
            notifyDataSetChanged();
        }
    }
}
